package academic.ui.client.presenter;


import java.util.ArrayList;

import academic.ui.client.CustomerServiceAsync;
import academic.ui.client.widgets.tools.Scrollable;
import ecommerce.soco.shared.GWT_Purchase;
import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.GwtEvent.Type;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.EventBus;
import com.gwtplatform.mvp.client.PresenterWidget;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.ContentSlot;
import com.gwtplatform.mvp.client.proxy.RevealContentHandler;

public class MyDealsPresenter extends PresenterWidget<MyDealsPresenter.MyView>
{

	private final CustomerServiceAsync service;
	
	@ContentSlot
	public static final Type<RevealContentHandler<?>> slot = new Type<RevealContentHandler<?>>();
	
	
	@Inject 
	public MyDealsPresenter(EventBus eventBus, MyView view,CustomerServiceAsync service) {
		super(eventBus, view);
		this.service = service;
	}

	
	public interface MyView extends View {

		void showMyDealsTab(ArrayList<GWT_Purchase> result);

		HasClickHandlers getShowAll();

		HasClickHandlers getShowGifts();

		void setLinkSelected(String string);

	}
	
	@Override
	public void onReveal() {
		Log.debug("MyDealsPresenter onReveal");
		service.getDealsPurchasedByCustomer(new AsyncCallback<ArrayList<GWT_Purchase>>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(ArrayList<GWT_Purchase> result) {
				getView().showMyDealsTab(result);
				getView().setLinkSelected("all");
				DeferredCommand.addCommand(new Command() {
					@Override
					public void execute() {
						Scrollable.initScrollable();
					}
				});
			}
		});
	}
	
	@Override
	public void onBind() {
		super.onBind();
		Log.debug("MyDealsPresenter onBind");
		registerHandler(getView().getShowAll().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				onReveal();
			}
			
		}));
		registerHandler(getView().getShowGifts().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				service.getGiftsPurchasedByCustomer(new AsyncCallback<ArrayList<GWT_Purchase>>() {

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						
					}

					@Override
					public void onSuccess(ArrayList<GWT_Purchase> result) {
						getView().showMyDealsTab(result);
						getView().setLinkSelected("gift");
						DeferredCommand.addCommand(new Command() {
							@Override
							public void execute() {
								Scrollable.initScrollable();
							}
						});
					}
					
				});
			}
			
		}));
	}
	
	@Override
	public void onUnbind() {
		super.onUnbind();
		
	}
	
	@Override
	public void onReset() {
		super.onReset();
		Log.debug("MyDealsPresenter onReset");
	}
	
	

}
